#include "controlpanel.h"
#include "ui_controlpanel.h"
#include <QDebug>
#include <QGraphicsDropShadowEffect>
#include <QMouseEvent>
#include <QString>
#include <qmovie.h>
#include "iconfont.h"

ControlPanel::ControlPanel(QWidget *parent) :
    QFrame(parent),
    ui(new Ui::ControlPanel),
    m_ctrl_state(false),
    m_ctrl_disable(false),
    cmd("{\"%1\":\"%2\"}")
{
    ui->setupUi(this);
    ui->lbl_ctrl->installEventFilter(this);
    setPlugsToView(parent);

    qDebug() << "this->parent()->objectName()" << this->parent()->objectName();
}

ControlPanel::~ControlPanel()
{
    delete ui;
}

bool ControlPanel::eventFilter(QObject *watched, QEvent *event)
{
    // 校验是否禁用点击
    if(m_ctrl_disable)
        return false;
    if(watched == ui->lbl_ctrl && event->type() == QEvent::MouseButtonPress){
        QMouseEvent *mouseEvent = static_cast<QMouseEvent *>(event);
        if (mouseEvent->button() == Qt::LeftButton)
        {
            m_ctrl_state = !m_ctrl_state;
            if(m_ctrl_state){
                On();
            }else{
                Off();
            }
            return true; // 事件已经被处理，不需要传递给目标对象
        }
    }

    // 对于其他事件，使用默认处理方式
    return QObject::eventFilter(watched, event);
}

void ControlPanel::setPlugsToView(QWidget *view)
{
    QVBoxLayout *layout = new QVBoxLayout(view);
    layout->setContentsMargins(2,2,2,2);
    layout->addWidget(this);
    view->setLayout(layout);
}

void ControlPanel::setGif(QString gif_path)
{
    m_gif_movie = new QMovie(gif_path);
    ui->lbl_gif->setMovie(m_gif_movie);
    ui->lbl_gif->setScaledContents(true);
}

void ControlPanel::Off()
{
    m_gif_movie->stop();
    setCtrl(QChar(0xe642),"000000");
    setStyle("#ffffff","#c0c0c0");
    sendData(cmd.arg(this->parent()->objectName()).arg("Off"));
    ui->lbl_state->setText("待\n机\n中");
    ui->lbl_state->setStyleSheet("color:#000000");
}

void ControlPanel::On()
{
    qDebug() << "On";
    m_gif_movie->start();
    setCtrl(QChar(0xe643),"#67c23a");
    setStyle("#ffffff","#67c23a");
    sendData(cmd.arg(this->parent()->objectName()).arg("On"));
    ui->lbl_state->setText("运\n行\n中");
    ui->lbl_state->setStyleSheet("color:#67c23a");
}

void ControlPanel::setState(QString state,QString color)
{
    ui->lbl_state->setText(state.split("", QString::SkipEmptyParts).join("\n"));
    QString style = "color:%1";
    ui->lbl_state->setStyleSheet(style.arg(color));
}

void ControlPanel::setTitel(QString titel, QString color)
{
    ui->lbl_titel->setText(titel);
    QString style = "color:%1";
    ui->lbl_titel->setStyleSheet(style.arg(color));
}

void ControlPanel::setCtrl(QChar icon, QString color)
{
    ui->lbl_ctrl->setFont(IconFont::IconFontInstance()->iconFont);
    ui->lbl_ctrl->setText(icon);
    QString style = "color:%1;font-size:36px;";
    ui->lbl_ctrl->setStyleSheet(style.arg(color));
}

void ControlPanel::disableCtrl(bool is_disable)
{
    // 禁用操作
    if(is_disable){
        m_ctrl_disable = true;
        ui->lbl_ctrl->setToolTip("操作禁用");
    }
    else{
        m_ctrl_disable = false;
        ui->lbl_ctrl->setToolTip("");
    }
}

void ControlPanel::setState(bool state)
{
    m_ctrl_state = state;
    if(m_ctrl_state){
        m_gif_movie->start();
        setCtrl(QChar(0xe643),"#67c23a");
        setStyle("#ffffff","#67c23a");
        ui->lbl_state->setText("运\n行\n中");
        ui->lbl_state->setStyleSheet("color:#67c23a");
    }else{
        m_gif_movie->stop();
        setCtrl(QChar(0xe642),"000000");
        setStyle("#ffffff","#c0c0c0");
        ui->lbl_state->setText("待\n机\n中");
        ui->lbl_state->setStyleSheet("color:#000000");
    }

}

void ControlPanel::setStyle(QString bac_color,QString shadow_color,qint64 radius)
{
    //设置具体阴影
    auto shadow_effect = new QGraphicsDropShadowEffect;
    shadow_effect->setOffset(0, 0);
    //阴影颜色
    shadow_effect->setColor(QColor(shadow_color));
    //阴影半径
    shadow_effect->setBlurRadius(radius);
    // 控件设置
    this->setGraphicsEffect(shadow_effect);
    QString style = "QFrame#%1{border:2px solid %2;border-radius:%3px;background-color: %4;}";
    this->setStyleSheet(style.arg(this->objectName()).arg(bac_color).arg(radius).arg(bac_color));
}

void ControlPanel::setBasicTemplate(QString bac_color, QString shadow_color, QString titel, QString state,QString gif,QString color)
{
    setStyle(bac_color,shadow_color);
    setTitel(titel,color);
    setState(state,color);
    setGif(gif);
    On();
    Off();
}
